import java.util.Arrays;
import java.util.HashMap;

/**
 * @program: LeetCode
 * @description: LeetCode : 349. 两个数组的交集
 * @author: WXY
 * @create: 2023-01-08 21:28
 * @Version 1.0
 **/
public class Num349_intersection {
    public static int[] intersection(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num : nums1) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        int[] ans = new int[nums1.length];
        int index = 0;
        for (int num : nums2) {
            if (map.containsKey(num)) {
                ans[index++] = num;
                map.remove(num);
            }
        }
        return Arrays.copyOfRange(ans, 0, index);
    }

}
